package com.zhongke.cotrun.mapper;


import com.zhongke.cotrun.model.pojo.DictEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.cache.annotation.Cacheable;

import java.util.List;

/**
 * 数据字典表
 *
 * @author lee
 * @email wawzj512541@gmail.com
 * @date 2019-10-10 09:41:28
 */
public interface DictDao {
    //新增
    //@Insert("insert into dict (name,type,code,value,order_num,remark,del_flag,parent_id) values (#{name},#{type},#{code},#{value},#{orderNum},#{remark},#{delFlag},#{parentId})")
    @Insert("insert into dict (type,code,value,order_num,remark,del_flag,parent_id) values (#{type},#{code},#{value},#{orderNum},#{remark},#{delFlag},#{parentId})")
    public boolean add(DictEntity entity);

    //删除
    @Update("update dict set del_flag = 1 where id = #{id};")
    boolean deleteById(@Param("id") Integer id);

    //修改
    public boolean update(DictEntity entity);

    //根据id搜索
    @Select("select id,type,code,value,order_num,remark,del_flag,parent_id from dict where del_flag = 0 and id = #{id} limit 1")
    public DictEntity getById(@Param("id") Integer id);

    //根据type搜索
    @Select("select id,type,code,value,order_num,remark,del_flag,parent_id from dict where del_flag = 0 and type = #{type} order by order_num")
    public List<DictEntity> getByType(@Param("type") String type);

    //根据type搜索
    @Cacheable(cacheNames = "sysConfig:dict:item", key = "#type")
    @Select("select id,type,value,remark from dict where del_flag = 0 and type = #{type} limit 1")
    public DictEntity getItemByType(@Param("type") String type);

    //搜索所有
    @Select("select id,type,code,value,order_num,remark,del_flag,parent_id from dict where del_flag = 0")
    public List<DictEntity> selectAll();

    //根据父id搜索
    @Select("select id,type,code,value,order_num,remark,del_flag,parent_id from dict where del_flag = 0 and parent_id =#{parentId} order by order_num")
    public List<DictEntity> getByParentId(@Param("parentId") Integer parentId);

    @Select("SELECT group_concat(value) FROM dict where code in (${Codes})")
    String selectValueInCodes(@Param("Codes")String Codes);

    //根据code搜索
    @Select("select id,type,code,value,order_num,remark,del_flag,parent_id from dict where del_flag = 0 and code = #{code} limit 1")
    DictEntity getByCode(@Param("code")String code);
}
